package org.hikari.qqbot.xiuxian.util;

import cn.hutool.core.date.DateUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * created at 2024-12-17 18:18:19
 *
 * @author yinchao
 * @version 1.0
 **/
public class MessageUtil {

    public static String matchAfterKeyword(String message, String keywords) {
        int i = message.indexOf(keywords);
        if (i == -1) {
            return null;
        }
        return message.substring(i + keywords.length());
    }

    public static Date parseExpectTime(String message) {
        String part = MessageUtil.matchPatternSingleGroup(message, "预计完成时间：([0-9]{2}时[0-9]{2}分[0-9]{2}秒)");
        if (part != null) {
            Date date = DateUtil.parse(DateUtil.today() + " " + part, "yyyy-MM-dd HH时mm分ss秒");
            if (date.before(new Date())) {
                return DateUtil.offsetDay(date, 1);
            }
            return date;
        }
        return null;
    }

    public static Integer parseTiLiTimes(String message) {
        String part = MessageUtil.matchPatternSingleGroup(message, "总共有体力次数【([0-9]+)】");
        if (part != null) {
            return Integer.parseInt(part);
        }
        return null;
    }

    public static String matchPatternSingleGroup(String message, String pattern) {
        return matchPatternSingleGroup(message, Pattern.compile(pattern));
    }

    public static String matchPatternSingleGroup(String message, Pattern pattern) {
        Matcher matcher = pattern.matcher(message);
        if (matcher.find()) {
            if (matcher.groupCount() == 1) {
                return matcher.group(1);
            }
            throw new IllegalArgumentException("Multiple pattern group! pattern = " + pattern.pattern());
        }
        return null;
    }

    public static List<List<String>> matchAllPatternGroup(String message, String pattern) {
        return matchAllPatternGroup(message, Pattern.compile(pattern));
    }

    public static List<List<String>> matchAllPatternGroup(String message, Pattern pattern) {
        List<List<String>> matches = new ArrayList<>();
        Matcher matcher = pattern.matcher(message);
        while (matcher.find()) {
            List<String> groups = new ArrayList<>();
            for (int i = 1; i <= matcher.groupCount(); i++) {
                groups.add(matcher.group(i));
            }
            matches.add(groups);
        }
        return matches;
    }

    public static List<String> matchAllPattern(String message, String pattern, int group) {
        return matchAllPattern(message, Pattern.compile(pattern), group);
    }

    public static List<String> matchAllPattern(String message, Pattern pattern, int group) {
        List<String> matches = new ArrayList<>();
        Matcher matcher = pattern.matcher(message);
        while (matcher.find()) {
            matches.add(matcher.group(group));
        }
        return matches;
    }

    public static String matchFirstPattern(String message, String pattern) {
        return matchFirstPattern(message, Pattern.compile(pattern), 0);
    }

    public static String matchFirstPattern(String message, String pattern, int group) {
        return matchFirstPattern(message, Pattern.compile(pattern), group);
    }

    public static String matchFirstPattern(String message, Pattern pattern, int group) {
        Matcher matcher = pattern.matcher(message);
        if (matcher.find()) {
            return matcher.group(group);
        }
        return null;
    }

    public static String getDisplaySendTime(Date date) {
        if (date == null) {
            return "";
        }
        return DateUtil.format(date, "yyyy年MM月dd日 HH时mm分ss秒");
    }

    public static boolean isTimesCommand(String command) {
        return matchFirstPattern(command, "[\\s][1-9][0-9]$") != null;
    }

    public static boolean isShiLianCommand(String command) {
        return command.endsWith(" 十连");
    }

}
